﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;
using Syncfusion.DocIO.DLS;
using Syncfusion.Mvc.Grid;
using Syncfusion.Mvc.Shared;
using Syncfusion.Mvc.Tools;
using Syncfusion.Pdf;
using Syncfusion.XlsIO;

namespace MvcApplication4.Controllers
{
    public class CadastroVacinacaoController : Controller
    {
        private BD_Vacinas2Context db = new BD_Vacinas2Context();

        //
        // GET: /CadastroVacinacao/

        public ActionResult Index()
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacinacao.Take(100).ToList();
            return View(data);
            //var tb_vacinacao = db.tb_Vacinacao.Include(t => t.LOTE).Include(t => t.tb_Funcionario).Include(t => t.tb_Paciente).Include(t => t.tb_UBS).Include(t => t.tb_Vacina);
            //return View(tb_vacinacao.ToList());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(PagingParams args)
        {
            IEnumerable data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacinacao.Take(100).ToList();
            return data.GridActions<tb_Vacinacao>();
        }

        public ActionResult ExportToExcel()
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacinacao.Take(200).ToList();
            return data.GridExportToExcel<tb_Vacinacao>("GridExcel.xlsx", ExcelVersion.Excel2010);
        }

        public ActionResult ExportToWord(PagingParams args)
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacinacao.Take(200).ToList();
            return data.GridExportToWord<tb_Vacinacao>("GridWord.doc", DocumentVersion.Word2007, args.ExportOption);
        }

        public ActionResult ExportToPDF(PagingParams args)
        {
            var data = new MvcApplication4.Models.BD_Vacinas2Context().tb_Vacinacao.Take(200).ToList();
            return data.GridExportToPDF<tb_Vacinacao>("GridPDF.pdf", PdfVersion.Version1_7, args.ExportOption);
        }

        //
        // GET: /CadastroVacinacao/Details/5

        public ActionResult Details(int id = 0)
        {
            tb_Vacinacao tb_vacinacao = db.tb_Vacinacao.Find(id);
            if (tb_vacinacao == null)
            {
                return HttpNotFound();
            }
            return View(tb_vacinacao);
        }

        //
        // GET: /CadastroVacinacao/Create

        public ActionResult Create()
        {
            var funcionarios = from s in db.tb_Funcionario
                               select s;

            var validaLote = from v in db.LOTEs
                             select v;

            var ubs = funcionarios;

            funcionarios = funcionarios.Where(s => s.Login_Fun.Equals(@User.Identity.Name));
            TempData["tempfun"] = funcionarios;
            validaLote = validaLote.Where(v => v.Validade >= DateTime.Now && v.Num_Doses > 1);

            List<SelectListItem> motivolist = new List<SelectListItem>();

            motivolist.Add(new SelectListItem { Text = "ROTINA", Value = "0", Selected = true });
            motivolist.Add(new SelectListItem { Text = "CAMPANHA", Value = "1"});

            ViewBag.ID_Lote = new SelectList(db.LOTEs, "ID_Lote", "ID_Lote");
            ViewBag.ID_Funcionario = new SelectList(funcionarios, "ID_Funcionario", "Nome_Funcionario",selectedValue: true);
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente");
            ViewBag.ID_Dependente = new SelectList(db.tb_Dependente, "ID_Dependente", "Nome_Dependente");
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS");
            ViewBag.ID_Vacina = new SelectList(db.tb_Vacina, "ID_Vacina", "Nome_Vacina");
            ViewBag.MotivoList = motivolist;

            DatePickerModel model = new DatePickerModel();

            model.DisplayDefaultDateOnLoad = true;

            ViewData["datemodel"] = model;

            return View();
        }

        //
        // POST: /CadastroVacinacao/Create

        [HttpPost]
        public ActionResult Create(tb_Vacinacao tb_vacinacao)
        {
                if (ModelState.IsValid)
                {
                    TempData["tempdata"] = DateTime.Now;
                    var tempdata = TempData["tempdata"] as DateTime?;
                    tb_vacinacao.Data_Vacinacao = tempdata;
                    db.tb_Vacinacao.Add(tb_vacinacao);
                    LOTE lot = db.LOTEs.Find(tb_vacinacao.ID_Lote);
                    lot.Num_Doses = lot.Num_Doses - 1;
                    db.Entry(lot).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

            ViewBag.ID_Lote = new SelectList(db.LOTEs, "ID_Lote", "ID_Lote", tb_vacinacao.ID_Lote);
            ViewBag.ID_Funcionario = new SelectList(db.tb_Funcionario, "ID_Funcionario", "Nome_Funcionario", tb_vacinacao.ID_Funcionario);
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente", tb_vacinacao.ID_Paciente);
            ViewBag.ID_Dependente = new SelectList(db.tb_Dependente, "ID_Dependente", "Nome_Dependente");
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS", tb_vacinacao.ID_UBS);
            ViewBag.ID_Vacina = new SelectList(db.tb_Vacina, "ID_Vacina", "Nome_Vacina", tb_vacinacao.ID_Vacina);
            return View(tb_vacinacao);
        }

        //
        // GET: /CadastroVacinacao/Edit/5

        public ActionResult Edit(int id = 0)
        {
            tb_Vacinacao tb_vacinacao = db.tb_Vacinacao.Find(id);
            if (tb_vacinacao == null)
            {
                return HttpNotFound();
            }

            var funcionarios = from s in db.tb_Funcionario
                               select s;

            var validaLote = from v in db.LOTEs
                             select v;

            var ubs = funcionarios;

            funcionarios = funcionarios.Where(s => s.Login_Fun.Equals(@User.Identity.Name));

            List<SelectListItem> motivolist = new List<SelectListItem>();

            motivolist.Add(new SelectListItem { Text = "ROTINA", Value = "0", Selected = true });
            motivolist.Add(new SelectListItem { Text = "CAMPANHA", Value = "1" });

            ViewBag.ID_Lote = new SelectList(db.LOTEs, "ID_Lote", "ID_Lote", tb_vacinacao.ID_Lote);
            ViewBag.ID_Funcionario = new SelectList(funcionarios, "ID_Funcionario", "Nome_Funcionario", tb_vacinacao.ID_Funcionario);
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente", tb_vacinacao.ID_Paciente);
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS", tb_vacinacao.ID_UBS);
            ViewBag.ID_Vacina = new SelectList(db.tb_Vacina, "ID_Vacina", "Nome_Vacina", tb_vacinacao.ID_Vacina);
            ViewBag.MotivoList = motivolist;
            return View(tb_vacinacao);
        }

        //
        // POST: /CadastroVacinacao/Edit/5

        [HttpPost]
        public ActionResult Edit(tb_Vacinacao tb_vacinacao)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tb_vacinacao).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ID_Lote = new SelectList(db.LOTEs, "ID_Lote", "ID_Lote", tb_vacinacao.ID_Lote);
            ViewBag.ID_Funcionario = new SelectList(db.tb_Funcionario, "ID_Funcionario", "Nome_Funcionario", tb_vacinacao.ID_Funcionario);
            ViewBag.ID_Paciente = new SelectList(db.tb_Paciente, "ID_Paciente", "Nome_Paciente", tb_vacinacao.ID_Paciente);
            ViewBag.ID_UBS = new SelectList(db.tb_UBS, "ID_UBS", "Nome_UBS", tb_vacinacao.ID_UBS);
            ViewBag.ID_Vacina = new SelectList(db.tb_Vacina, "ID_Vacina", "Nome_Vacina", tb_vacinacao.ID_Vacina);
            return View(tb_vacinacao);
        }

        //
        // GET: /CadastroVacinacao/Delete/5

        public ActionResult Delete(int id = 0)
        {
            tb_Vacinacao tb_vacinacao = db.tb_Vacinacao.Find(id);
            if (tb_vacinacao == null)
            {
                return HttpNotFound();
            }
            return View(tb_vacinacao);
        }

        //
        // POST: /CadastroVacinacao/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tb_Vacinacao tb_vacinacao = db.tb_Vacinacao.Find(id);
            db.tb_Vacinacao.Remove(tb_vacinacao);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}